package com.example.task1.ui.AllService.word;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.WordTheBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import retrofit2.Call;

public class WordSearchActivity extends AppCompatActivity {

    private RecyclerView rvList;
    private WordTheListAdapter wordTheListAdapter;
    private LinearLayoutManager linearLayoutManager;
    private String input;
    private TextView end;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_search);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("搜索");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        input = getIntent().getStringExtra("input");
        initView();
        initData();
        setAdapter();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/job/company/list?companyName=" + input);
        Api.send(apiRespCall, WordTheBean.class, new CallBack<WordTheBean>() {
            @Override
            public void success(WordTheBean data) {
                if (data.getRows().size() == 0) {
                    end.setVisibility(View.VISIBLE);
                }else {
                    wordTheListAdapter.setData(data.getRows());
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void setAdapter() {
        wordTheListAdapter = new WordTheListAdapter(this);
        linearLayoutManager = new LinearLayoutManager(this);
        rvList.setLayoutManager(linearLayoutManager);
        rvList.setAdapter(wordTheListAdapter);
    }


    private void initView() {
        rvList = (RecyclerView) findViewById(R.id.rv_list);
        end = (TextView) findViewById(R.id.end);
    }
}